package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetDistributorLevelCountReq;
import com.shop2cn.iapi.sqdistributor.service.GetDistributorLevelCountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;

public class Ts_GetDistributorLevelCount {
	
	//java接口
	private static GetDistributorLevelCountReq getDistributorLevelCountReq;
	private static GetDistributorLevelCountCall getDistributorLevelCountCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商户某个级别的分销员的数量");
	}

	@Before
	public void caseUp() {
		getDistributorLevelCountReq = new GetDistributorLevelCountReq();
		getDistributorLevelCountCall = new GetDistributorLevelCountCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getDistributorLevelCountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		int count = new JSONObject(getDistributorLevelCountCall.getReturnData())
				.getJSONObject("result").getInt("count");
		
		DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();

		List<DistributorRelation> actualList = distributorRelationWrapper
				.selectByMerchantIdAndLevel(
						getDistributorLevelCountReq.getMerchantId(),
						getDistributorLevelCountReq.getLevel());

		Logger.verifyEquals(count, actualList.size(), "验证count");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributorLevelCount_001() {
		Logger.start(true, "获取商户某个级别的分销员的数量");
		
		try {
			
			getDistributorLevelCountReq.setLevel(1);
			getDistributorLevelCountReq.setMerchantId(500861049L);
			
			getDistributorLevelCountCall.setData(getDistributorLevelCountReq);
			getDistributorLevelCountCall.callService();
			
			Logger.verifyEquals(true, getDistributorLevelCountCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributorLevelCount_002() {
		Logger.start(true, "获取商户某个级别的分销员的数量为0");
		
		try {
			
			getDistributorLevelCountReq.setLevel(1123124);
			getDistributorLevelCountReq.setMerchantId(500861049L);
			
			getDistributorLevelCountCall.setData(getDistributorLevelCountReq);
			getDistributorLevelCountCall.callService();
			
			Logger.verifyEquals(true, getDistributorLevelCountCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
